<template>
  <div>
    <ZgPage
      ref="page"
      title="轮播图管理"
      addBtnName="新增轮播图"
      :addOpt="{title:'新增轮播图'}"
      :updateOpt="{title:'修改轮播图'}"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
    >
      <template slot="columns">
        <a-table-column dataIndex="name" title="名称"></a-table-column>
        <a-table-column :width="150" title="图片数量">
          <template slot-scope="text,record">
            <ImgList :bannerId="record.id"></ImgList>
          </template>
        </a-table-column>
        <a-table-column :width="150" title="切换速度">
          <template slot-scope="text,record">
            {{ record.speed }}&nbsp;秒
          </template>
        </a-table-column>
        <a-table-column :width="150" title="状态">
          <template slot-scope="text,record">
            <div v-if="record.isUse">启用</div>
            <div v-else>禁用</div>
          </template>
        </a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
import ImgList from './ImgList'
export default {
  components: {
    Add, Update, ImgList
  },
  data () {
    return {
    }
  },
  methods: {
    onSearch (param) {
      this.$http.get('/zg-system/banner/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onDel (ids) {
      this.$http.post('/zg-system/banner/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/banner/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/banner/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
